﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.BD.VMTool.Untils
{
    public class VMUdpUntil
    {
        public static bool SendPointTarget(string ip, string port, Datas.Solution solution)
        {
            if (solution != null && solution.Mapping != null)
            {
                bool isSendsucceed = true;
                foreach (Datas.Mapping mapping in solution.Mapping)
                { 
                    string strUdp = string.Format("zi{0}o{1}", mapping.VideoSourceId, mapping.VideoMatrixId);
                    byte[] udpData = System.Text.Encoding.Default.GetBytes(strUdp);
                    isSendsucceed =  SendUdpClient(ip, port, udpData, udpData.Length);
                }
                return isSendsucceed;
            }
            return false;
        }

        public static byte[] MappingToByte(Datas.Mapping mapping)
        {
            string strUdp = string.Format("zi{0}o{1}", mapping.VideoSourceId, mapping.VideoMatrixId);
            byte[] udpData = System.Text.Encoding.Default.GetBytes(strUdp);
            return udpData;
        }

        /// <summary>
        /// 将solution转成byte[]
        /// </summary>
        /// <param name="solution"></param>
        /// <returns></returns>
        public static byte[] SolutionToByte(Datas.Solution solution)
        {
           
            int size = Marshal.SizeOf(solution);
            byte[] bytes = new byte[size];
           
            IntPtr structPtr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(solution, structPtr, false);
            Marshal.Copy(structPtr, bytes, 0, size);
            Marshal.FreeHGlobal(structPtr);
            return bytes;
        }

        /// <summary>
        /// 发送到地址和端口
        /// </summary>
        /// <returns></returns>
        public static bool SendPointTarget(string ip, string port,string solutionName)
        {
            Console.WriteLine(string.Format("发送到默认的地址和端口：{0}:{1}", ip, port));
            List<Datas.Solution> ListSolution = VMSolutionUntil.GetListSolutionByName(solutionName);
            if (ListSolution != null)
            {
                bool isSendsucceed = true;
                foreach (Datas.Solution solution in ListSolution)
                {
                    foreach (Datas.Mapping mapping in solution.Mapping)
                    {
                        string strUdp = string.Format("zi{0}o{1}", mapping.VideoSourceId, mapping.VideoMatrixId);
                        byte[] udpData = System.Text.Encoding.Default.GetBytes(strUdp);
                        Console.WriteLine(string.Format("发送指令:{0}", strUdp));
                       isSendsucceed = SendUdpClient(ip, port, udpData, udpData.Length);
                    }

                }
                return isSendsucceed;
            }
            return false;
        }

        public static bool SendUdpClient(string ip, string port, byte[] sendData, int bytes)
        {
            try
            {
                IPAddress hostIP = IPAddress.Parse(ip);
                IPEndPoint host = new IPEndPoint(hostIP, Convert.ToInt32(port));
                UdpClient udpClient = new UdpClient();

                udpClient.Send(sendData, bytes, host);
            }
            catch (Exception ex)
            {
                return false;
                //throw ex;
            }
            return true;
        }

    }
}
